﻿#ifndef NETWORK_PACKET_H
#define NETWORK_PACKET_H

#include <QByteArray>
#include <QJsonObject>

#include "chessDef.h"
#include "step.h"

struct NetworkHeader {
    uint32_t magic;     // 魔数 0x88E4C5AA
    uint8_t version;    // 协议版本 0x01
    uint8_t algorithm;  // 0x00=JSON 0x01=Protobuf
    uint8_t command;    // 指令类型
    uint32_t length;    // 数据长度
};

class NetworkPacket {
public:
    static QByteArray buildMatchRequest(QString playerId,
		int gameTimeLimitMs, int stepTimeLimitMs);
	static QByteArray buildMoveRequest(QString roomId, QString playerId,
		QSharedPointer<Step> move);
	/*static QByteArray buildMatchResponse(bool success, const QString& reason);
	static QByteArray buildMatchCancel();
	static QByteArray buildMatchCancelResponse(bool success, const QString& reason);
	static QByteArray buildMatchStart(const QString& redPlayer, const QString& blackPlayer);
	static QByteArray buildMatchStartResponse(bool success, const QString& reason);
	static QByteArray buildMatchEnd(const QString& winner);
	static QByteArray buildMatchEndResponse(bool success, const QString& reason);
	static QByteArray buildMatchMove(const QString& player, const QString& move);
	static QByteArray buildMatchMoveResponse(bool success, const QString& reason);
	static QByteArray buildMatchRetrack(const QString& player);
	static QByteArray buildMatchRetrackResponse(bool success, const QString& reason);
	static QByteArray buildMatchTimeout(const QString& player);
	static QByteArray buildMatchTimeoutResponse(bool success, const QString& reason);
	static QByteArray buildMatchError(const QString& reason);
	static QByteArray buildMatchErrorResponse(bool success, const QString& reason);
	static QByteArray buildMatchPing();
	static QByteArray buildMatchPingResponse(bool success, const QString& reason);
	static QByteArray buildMatchQuit();
	static QByteArray buildMatchQuitResponse(bool success, const QString& reason);
	static QByteArray buildMatchReconnect();
	static QByteArray buildMatchReconnectResponse(bool success, const QString& reason);*/

	static QByteArray headerToSmallOrBigEndian(const NetworkHeader& header,
		QDataStream::ByteOrder order);
private:
    static QByteArray buildPacket(CommandType cmd, const QJsonObject& data);
};

#endif // NETWORK_PACKET_H